#ifndef CT_I2C_H
#define CT_I2C_H

#include "ct_type.h"

/* IMPORTANT !! This define for printf code only. Disable this define to reduce code size. */
#define print_function

#define CT_I2C_CLOCK 128
#define CT_I2C_WR    0
#define CT_I2C_RD    1

#define CT_LOOP_SIZE 10

// Master mode status macros
#define CT_I2C_MSTR_START        0x08    // Master start signal
#define CT_I2C_MSTR_REP_START    0x10    // Master repeated start
#define CT_I2C_MSTR_TX_ADDR_ACK  0x18    // Master TX address with ACK
#define CT_I2C_MSTR_TX_ADDR_NACK 0x20    // Master TX address with NACK
#define CT_I2C_MSTR_TX_DATA_ACK  0x28    // Master TX data with ACK
#define CT_I2C_MSTR_TX_DATA_NACK 0x30    // Master TX data with NACK
#define CT_I2C_MSTR_ARB_LOST     0x38    // Master arbitration lost
#define CT_I2C_MSTR_RX_ADDR_ACK  0x40    // Master RX address with ACK
#define CT_I2C_MSTR_RX_ADDR_NACK 0x48    // Master RX address with NACK
#define CT_I2C_MSTR_RX_DATA_ACK  0x50    // Master RX data with ACK
#define CT_I2C_MSTR_RX_DATA_NACK 0x58    // Master RX data with NACK
#define CT_I2C_MSTR_BUS_ERR      0x00    // Master bus error

// Slave mode status macros
#define CT_I2C_SLV_TX_STP       0xA0    // Slave TX start/stop
#define CT_I2C_SLV_TX_ADDR_ACK  0xA8    // Slave TX address with ACK
#define CT_I2C_SLV_ARB_LOST     0xB0    // Slave arbitration lost
#define CT_I2C_SLV_TX_DATA_ACK  0xB8    // Slave TX data with ACK
#define CT_I2C_SLV_TX_DATA_NACK 0xC0    // Slave TX data with NACK
#define CT_I2C_SLV_TX_LAST_ACK  0xC8    // Slave TX last data with ACK
#define CT_I2C_SLV_RX_ADDR_ACK  0x60    // Slave RX address with ACK
#define CT_I2C_SLV_RX_ARB_LOST  0x68    // Slave RX arbitration lost
#define CT_I2C_SLV_RX_DATA_ACK  0x80    // Slave RX data with ACK
#define CT_I2C_SLV_RX_DATA_NACK 0x88    // Slave RX data with NACK
#define CT_I2C_SLV_GC_ADDR_ACK  0x70    // Slave general call addr with ACK
#define CT_I2C_SLV_GC_ARB_LOST  0x78    // Slave general call arbitration lost
#define CT_I2C_SLV_GC_DATA_ACK  0x90    // Slave general call data with ACK
#define CT_I2C_SLV_GC_DATA_NACK 0x98    // Slave general call data with NACK
#define CT_I2C_BUS_FREE         0xF8    // Bus free

// =============================================================================
// Type definitions
// =============================================================================
// Data structure for I2C transactions
typedef struct {
    uint8_t u8_data;
    uint8_t error;
} xdata i2c_trans_t;

typedef struct {
    uint8_t clock;          // Specify I2C clock
    uint8_t SlaveAddr;      // Specify slave address
    uint8_t intt_enable;    // Enable interrupt
    uint8_t status;         // Status of I2C
} ct_i2c_init_t;

// Status of I2C
typedef enum {
    CT_I2C_OK      = 0,
    CT_I2C_ERR     = -1,
    CT_I2C_BUSY    = -2,
    CT_I2C_TIMEOUT = -3
} ct_i2c_status_t;

//// I2C操作状态码
// #define CT_I2C_SUCCESS 0
// #define CT_I2C_FAIL    1

//// I2C读写方向
// #define CT_I2C_WRITE 0x00
// #define CT_I2C_READ  0x01

//// I2C配置结构体
// typedef struct {
//     uint32_t clockRate;       // I2C时钟频率
//     uint8_t  slaveAddress;    // I2C从设备地址
// } ct_i2c_config_t;

//// I2C传输结构体
// typedef struct {
//     uint8_t *data;      // 数据指针
//     uint32_t length;    // 数据长度
//     uint8_t  error;     // 错误状态
// } ct_i2c_transfer_t;

// Function prototypes
void ct_i2c_Init(void);
int  ct_i2c_Read(uint8_t reg_addr, i2c_trans_t *i2c_readx);
int  ct_i2c_Write(uint8_t reg_addr, uint8_t u8data, i2c_trans_t *i2c_write);

// ct_i2c_status_t ct_i2c_init(ct_i2c_config_t *config);
// ct_i2c_status_t ct_i2c_master_transmit(uint8_t address, const uint8_t *data, uint32_t length);
// ct_i2c_status_t ct_i2c_master_receive(uint8_t address, uint8_t *data, uint32_t length);

#endif /* CT_I2C_H */